package leecode

func pathTarget(root *TreeNode, target int) (result [][]int) {
	if root == nil {
		return
	}
	temp := make([]int, 0)
	var backtracking153 func(node *TreeNode, sum int)
	backtracking153 = func(node *TreeNode, sum int) {
		if node == nil {
			return
		}
		temp = append(temp, node.Val)
		sum += node.Val
		if sum == target && node.Left == nil && node.Right == nil {
			// 创建一个 temp 的副本，添加到 result 中
			tempCopy := make([]int, len(temp))
			copy(tempCopy, temp)
			result = append(result, tempCopy)
		}
		backtracking153(node.Left, sum)
		backtracking153(node.Right, sum)
		temp = temp[:len(temp)-1]
		sum = sum - node.Val
	}
	backtracking153(root, 0)
	return
}
